#include "StaticDungeons.h"

DungeonsStatic* StaticDungeons::getDungeonsInfo( int dungeonsID )
{
	assert(dungeonsInfoMap.count(dungeonsID) != 0);
	return dungeonsInfoMap[dungeonsID];
}

TaskStatic* StaticDungeons::getTaskInfo( int dungeonsID, int floorID, int taskID )
{
	FloorStatic* floor = getFloorInfo(dungeonsID, floorID);
	return floor->getTask(taskID);
}

FloorStatic* StaticDungeons::getFloorInfo( int dungeonsID, int floorID )
{
	DungeonsStatic* dungeons = getDungeonsInfo(dungeonsID);
	return dungeons->getFloor(floorID);
}

void StaticDungeons::parse()
{
	const char* path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("config/Dungeon.xml");

	xmlDocPtr dungeonDoc = xmlReadFile(path, "utf-8", XML_PARSER_EOF);
	if(NULL == dungeonDoc)
		CCLOG("Dungeons xml parse failed!");

	xmlNodePtr dungeonRootNode = xmlDocGetRootElement(dungeonDoc);
	if(NULL == dungeonRootNode)
		CCLOG("Dungeons xml root null!");

	xmlNodePtr dungeonChildNode = dungeonRootNode->last->last;

	path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("config/Task.xml");

	xmlDocPtr taskDoc = xmlReadFile(path, "utf-8", XML_PARSER_EOF);
	if(NULL == taskDoc)
		CCLOG("Task xml parse failed!");

	xmlNodePtr taskRootNode = xmlDocGetRootElement(taskDoc);
	if(NULL == taskRootNode)
		CCLOG("Task xml root null!");

	xmlNodePtr taskChildNode = taskRootNode->last->last;

	xmlNodePtr curNode = taskChildNode;
	while(NULL != curNode)
	{
		if (curNode->type == XML_COMMENT_NODE)break;
		TaskStatic* dataVO = new TaskStatic();
		dataVO->taskID = attriToInt(curNode, "id");
		dataVO->energy = attriToInt(curNode,"power");
		dataVO->name = attriToChar(curNode,"name");
		dataVO->desc = attriToChar(curNode,"description");
		dataVO->bossID = attriToInt(curNode,"boss");
		dataVO->drop = attriToInt(curNode,"drop");
		std::string s1 = attriToChar(curNode,"exp");
		s1 = s1.substr(1, s1.length()-2);
		std::vector<std::string> idList = StringUtil::split(s1,", ");
		dataVO->expMin = charToInt(idList[0].c_str());
		dataVO->expMax = charToInt(idList[1].c_str());
		s1 = attriToChar(curNode,"coin");
		s1 = s1.substr(1, s1.length()-2);
		idList = StringUtil::split(s1,", ");
		dataVO->coinMin = charToInt(idList[0].c_str());
		dataVO->coinMax = charToInt(idList[1].c_str());
		taskInfoMap[dataVO->taskID] = dataVO;
		curNode = curNode->prev;
	}

	DungeonsStatic* dungeonsData = new DungeonsStatic();
	dungeonsData->dungeonsID = 1;
	curNode = dungeonChildNode;
	while(NULL != curNode)
	{
		if (curNode->type == XML_COMMENT_NODE)break;
		FloorStatic* floorData = new FloorStatic();
		floorData->floorID = attriToInt(curNode,"id");
		floorData->name = attriToChar(curNode,"name");
		floorData->desc = attriToChar(curNode,"description");
		std::string s1 = attriToChar(curNode,"task");
		s1 = s1.substr(1, s1.length()-2);
		std::vector<std::string> idList = StringUtil::split(s1,", ");
		for (int i = 0; i < idList.size(); i++)
		{
			TaskStatic* taskData = taskInfoMap[strToInt(idList[i])];
			if (NULL != taskData)
				floorData->taskList[taskData->taskID] = taskData;
		}
		dungeonsData->floorList[floorData->floorID] = floorData;
		curNode = curNode->prev;
	}
	dungeonsInfoMap[dungeonsData->dungeonsID] = dungeonsData;
}
